package com.ruoyi.operation.order.management.service.impl;

import com.ruoyi.operation.order.management.domain.Management;
import com.ruoyi.operation.order.management.mapper.IManagementMapper;
import com.ruoyi.operation.order.management.service.IManagementService;
import com.ruoyi.operation.order.util.ExportExcelUtil;
import net.bytebuddy.implementation.auxiliary.AuxiliaryType;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author wangzhiwei phone(WeChat)):15062775766
 * @date 2020/5/3 17:05
 */
@Service
public class ManagementServiceImpl implements IManagementService {

    @Autowired
    IManagementMapper iManagementMapper;

    /**
     * 分页查查询所有的营销数据
     * @param management
     * @return
     */
    @Override
    public List<Management> findAll(Management management) {
        return iManagementMapper.findAll(management);
    }

    /**
     * 修改联系状态
     * @param management
     * @return
     */
    @Override
    public int changeStatus(Management management) {
        return iManagementMapper.changeStatus(management);
    }

    /**
     * 修改打开的状态
     * @param dataId
     */
    @Override
    public int changeIsOPen(String dataId) {
        return iManagementMapper.changeIsOPen(dataId);
    }

    /**
     * 根据id查询对应的对象
     * @param dataId
     * @return
     */
    @Override
    public Management findById(String dataId) {
        return iManagementMapper.findById(dataId);
    }

    /**
     * 改变状态
     * @param management
     * @return
     */
    @Override
    public int noContact(Management management) {
        return iManagementMapper.noContact(management);
    }

    /**
     * 查询当前的客户联系的次数
     * @param dataId
     * @return
     */
    @Override
    public int contacNumber(String dataId) {
        return iManagementMapper.contacNumber(dataId);
    }

    /**
     * 导出Excel表格
     * @param management
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void exportExcel(Management management, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Management> orderDataList = iManagementMapper.exportExcel(management);
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建导的Excel表的名字
        //sheet名
        HSSFSheet sheet = workbook.createSheet("客户数据表");
        // 设置导出文件的名字和导出的格式
        String fileName = "客户数据表"+".xls";
        String agent = request.getHeader("USER-AGENT");
        if(agent != null && agent.toLowerCase().indexOf("firefox") > 0)
        {
            fileName = "=?UTF-8?B?" + (new String(Base64Utils.encodeToString(fileName.getBytes("UTF-8")))) + "?=";
        } else {
            fileName =  java.net.URLEncoder.encode(fileName, "UTF-8");
        }
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 新增数据行，设置单元格数据
        int rowNum  = 1;
        // 定义导出Excel的表格
        String[] title = {"客户姓名","旺旺号","联系次数","商品名称","联系方式","地址","导入时间","备注","操作人"};
        String[] colList = {"customerName","wangwangNumber","contactsNumber","productName","customerPhone","customerAddress","enterDate","remark","createBy"};
        HSSFRow row = sheet.createRow(0);
        // 在Excel表中添加表头
        for (int i = 0;i<title.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(title[i]);
            cell.setCellValue(text);
        }
        // 在表中存放查询到的数据到对应的列
        for (Management c: orderDataList){
            HSSFRow row1 = sheet.createRow(rowNum);
            int j = 0;
            for(String s:colList){
                try {
                    row1.createCell(j).setCellValue((String)getGetMethod(c, s));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                j++;
            }
            rowNum++;
        }
        ExportExcelUtil.outPut(response,workbook,fileName);
    }

    /**
     * 根据属性，获取get方法
     * @param ob 对象
     * @param name 属性名
     * @return
     * @throws Exception
     */
    private Object getGetMethod(Object ob , String name)throws Exception{
        Method[] m = ob.getClass().getMethods();
        for(int i = 0;i < m.length;i++){
            if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){
                return m[i].invoke(ob);
            }
        }
        return null;
    }
}
